/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.gis.eemc.web;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.faces.context.FacesContext;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.gis.eemc.service.FacultyServiceImpl;
import net.gis.eemc.service.DepartmentServiceImpl;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Faculty;

/**
 *
 * @author Sergey
 */
@ManagedBean(name="editDepartmentBean")
@ViewScoped
public final class EditDepartmentBean {
    private Map<Integer, Faculty> facultyMap = null;
    private Integer selectedFaculty = null;
    private Department department = null;
    private String title = null;
    private String shortName = null;
    private DepartmentServiceImpl depSrv = null;
    private FacultyServiceImpl facSrv = null;
    private List<SelectItem> selectFaculties = null;

    private void fillSelectFaculties() {
        selectFaculties = new ArrayList<SelectItem>();
        facultyMap = new HashMap<Integer, Faculty>();
        for (Faculty fac : facSrv.getFaculties()) {
            facultyMap.put(fac.getIdfaculty(), fac);
            selectFaculties.add(new SelectItem(fac.getIdfaculty(), fac.getShortName()));
        }
    }

    /** Creates a new instance of EditDepartmentBean */
    public EditDepartmentBean() throws Exception {
        facSrv = new FacultyServiceImpl();
        depSrv = new DepartmentServiceImpl();

        FacesContext context = FacesContext.getCurrentInstance();
        String idDep = context.getExternalContext().getRequestParameterMap().get("id");

        if (idDep != null) {
            department = depSrv.getDepartments(new Integer(idDep));
            if (department != null) {
                title = department.getTitle();
                shortName = department.getShortName();
                selectedFaculty = department.getFaculty().getIdfaculty();
            } else {
                throw new Exception ("Incorrect ID was passed");
            }
        } else {
            throw new Exception("No ID was passed to the page");
        }
        
        fillSelectFaculties();
    }

     public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public List<SelectItem> getSelectFaculties() {
        return selectFaculties;
    }

    public Integer getSelectedFaculty() {
        return selectedFaculty;
    }

    public void setSelectedFaculty(Integer faculty) {
        this.selectedFaculty = faculty;
    }

    public void update() {
        if (selectedFaculty != null && title != null && shortName != null) {
            department.setFaculty(facultyMap.get(selectedFaculty));
            department.setTitle(title);
            department.setShortName(shortName);
            department.setFaculty(facultyMap.get(selectedFaculty));
            
            depSrv.update(department);
        }
    }

}
